package com.boc.address.presenter;

import com.boc.address.contract.AddAddressContract;
import com.boc.address.model.AddAddressModel;
import com.google.gson.Gson;

import demo.bocweb.com.sdk.base.bean.BaseBean;
import demo.bocweb.com.sdk.baseApi.AppUrl;
import demo.bocweb.com.sdk.baseApi.BaseErrorObserver;
import demo.bocweb.com.sdk.baseApi.BaseObserver;
import demo.bocweb.com.sdk.baseApi.DistrictBean;
import demo.bocweb.com.sdk.config.AppConfig;
import demo.bocweb.com.sdk.util.ACache;
import demo.bocweb.com.sdk.util.AppUtils;
import demo.bocweb.com.sdk.util.Loading;
import demo.bocweb.com.sdk.util.ToastyUtil;
import okhttp3.RequestBody;

/**
 * Created by zhelun on 2018/2/2.
 */

public class AddAddressPresenter extends AddAddressContract.IAddAddressPresenter {

    @Override
    public AddAddressContract.IAddAddressModel getModel() {
        return AddAddressModel.newInstance();
    }

    @Override
    public void onStart() {

    }

    @Override
    public void addAddress(RequestBody request) {
        if (mIModel == null || mIView == null)
            return;

        mRxManager.register(mIModel.addAddress(request)
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    public void setData(BaseBean baseBean) {
                        Loading.dismiss();
                        if (mIView == null)
                            return;
                        if (baseBean.getReturnCode() == AppUrl.SuccessCode) {

                            mIView.addSuccess();
                        } else {
                            ToastyUtil.showError(baseBean.getMsg());
                        }
                    }
                }, new BaseErrorObserver()));
    }

    @Override
    public void getDistrictBean() {
        if (mIModel == null || mIView == null)
            return;
        if (ACache.get(AppUtils.getContext()).getAsString(AppConfig.citybean) != null) {
            String cityJson = ACache.get(AppUtils.getContext()).getAsString(AppConfig.citybean);
            DistrictBean bean = new Gson().fromJson(cityJson, DistrictBean.class);
            mIView.updateDistrict(bean);
        } else {
            mRxManager.register(mIModel.getDistrictBean()
                    .subscribe(new BaseObserver<BaseBean<DistrictBean>>() {
                        @Override
                        public void setData(BaseBean<DistrictBean> beanBaseBean) {
                            Loading.dismiss();
                            if (mIView == null)
                                return;
                            if (beanBaseBean.getReturnCode() == AppUrl.SuccessCode) {
                                ACache.get(AppUtils.getContext()).put(AppConfig.citybean, new Gson().toJson(beanBaseBean.getData()));
                                mIView.updateDistrict(beanBaseBean.getData());
                            } else {
                                ToastyUtil.showError(beanBaseBean.getMsg());
                            }

                        }
                    }, new BaseErrorObserver()));
        }

    }
}
